import Vue from "vue";
import Router from "vue-router";
import $store from "./store/index";
import Login from "@views/Login";
import NotDefined from "@views/NotDefined";
import Loading from "@views/Loading";
import Pay from "@views/Pay";
import Manage from "@views/Manage";
import Turnover from "@views/Turnover";
import toLogin from "@libs/login";
// import { tr } from "date-fns/locale";

Vue.use(Router);

const router = new Router({
    mode: "history",
    routes: [
        {
          path: "/login",
          name: "Login",
          meta: {
              title: "登录",
              keepAlive: true,
              home: false
          },
          component: Login
        },
        {
            path: "/",
            name: "Manage",
            meta: {
                title: "经营报表",
                keepAlive: true,
                footer: true,
                backgroundColor: "#fff",
                auth: true
            },
            component: Manage
        },
        {
            path: "/turnover",
            name: "Turnover",
            meta: {
                title: "交易流水",
                keepAlive: true,
                footer: true,
                backgroundColor: "#fff",
                auth: true
            },
            component: Turnover
        },
        {
            path: "/pay",
            name: "Pay",
            meta: {
                title: "支付",
                keepAlive: true,
                footer: false,
                backgroundColor: "#fff",
                auth: true
            },
            component: Pay
        },
        {
            path: "/auth/:url",
            name: "Loading",
            meta: {
                title: " 加载中",
                keepAlive: true
            },
            component: Loading
        },
        {
            path: "*",
            name: "NotDefined",
            meta: {
                title: "页面找不到",
                keepAlive: true,
                home: false,
                backgroundColor: "#F4F6FB"
            },
            component: NotDefined
        }
    ],
    scrollBehavior(to, from) {
        from.meta.scrollTop = window.scrollY;
        return {x: 0, y: to.meta.scrollTop || 0};
    }
});

const {back, replace} = router;

router.back = function () {
    this.isBack = true;
    back.call(router);
};
router.replace = function (...args) {
    this.isReplace = true;
    replace.call(router, ...args);
};

router.beforeEach((to, form, next) => {
    const {title, backgroundColor, footer, home, auth} = to.meta;
    console.log(to.name, form.name);
    if (auth === true && !$store.state.app.token) {
        if (form.name === "Login") return;
        return toLogin(true, to.fullPath);
    }
    document.title = title || process.env.VUE_APP_NAME || "吉安xxx物业公司";
    //判断是否显示底部导航
    footer === true ? $store.commit("SHOW_FOOTER") : $store.commit("HIDE_FOOTER");

    //控制悬浮按钮是否显示
    home === false ? $store.commit("HIDE_HOME") : $store.commit("SHOW_HOME");

    $store.commit("BACKGROUND_COLOR", backgroundColor || "#F5F5F5");

    if (auth) {
        // $store.dispatch("USERINFO").then(() => {
        //     next();
        // });
        next();
    } else next();
});

export default router;
